package com.abigdreamer.im.server.ui.desktop.internal;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;

import com.abigdreamer.im.server.tools.ServerUtils;
import com.abigdreamer.im.server.ui.desktop.DeskTopConfig;
import com.abigdreamer.im.server.ui.desktop.ItemConfig;
import com.abigdreamer.im.server.ui.resourse.ResourseMannager;

public class WindowItem extends JLabel implements MouseListener{

	private JPopupMenu pop = null; // 弹出菜单
	private JMenuItem open = null, property = null, delete = null; // 三个功能菜单
	private EIMWindow window;
	
	public WindowItem(){
		this.addMouseListener(this);
		initPopMenu();
	}
	
	public void initPopMenu(){
		 pop = new JPopupMenu(); // 弹出菜单
		 open = new JMenuItem("打开");
		 property = new JMenuItem("属性"); 
		 delete = new JMenuItem("删除"); 
		 pop.add(open);
		 pop.add(new JSeparator());
		 pop.add(property);
		 pop.add(new JSeparator());
		 pop.add(delete);
		 this.add(pop);
		 open.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					menuAction(e);
				}
			});
		 property.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						menuAction(e);
					}
				});
		 delete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
						menuAction(e);
					}
				});
	}
	private void menuAction(ActionEvent e) {
		String str = e.getActionCommand();
		if (str.equals("打开")){
			
		}else if(str.equals("属性")){
			
		}else if(str.equals("删除")){
			Object[] options = {"确定","取消","帮助"}; 
			int response=JOptionPane.showOptionDialog(this, "您确定删除没?删除了就不可恢复！", "确定删除",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); 
			if(response==0) 
			{   window.remove(this);
			    window.repaint();
			    }
			else if(response==1) 
			{ //this.setTitle("您按下了第Cancel按钮 "); 
			}
			else if(response==2) 
			{ 
		    //this.setTitle("您按下了第Help按钮 "); 
			} 
		}
	}
	public  void mouseEntered(MouseEvent e){
		lableEntered();
	}

	public  void mouseExited(MouseEvent e){
		thisExited();
	}
	
	private  void lableEntered(){
		this.setBackground(Color.BLUE);
		this.setBounds(this.getX()+2, this.getY()+2, this.getWidth(), this.getHeight());
	}
	
	private  void thisExited(){
		this.setBounds(this.getX()-2, this.getY()-2, this.getWidth(), this.getHeight());
	}
	/**
	 * 
	 * @param iconName
	 * 这个iconName必须在ResourseMannager(ui.resourse)报下面否则会找不到
	 */
	public void setIcon(String iconName){
		ImageIcon icon=ResourseMannager.getIconUrlByName(iconName);
		this.setIcon(icon);
	}
	
	
	public  void mouseClicked(MouseEvent e){
		if (e.getButton() == MouseEvent.BUTTON3) {
			pop.show(this, e.getX(), e.getY());
			return;
		}
	}

	public void mousePressed(MouseEvent e) {
		
	}

	public void mouseReleased(MouseEvent e) {
		
	}

	public EIMWindow getWindow() {
		return window;
	}

	public void setWindow(EIMWindow window) {
		this.window = window;
	}

}
